Component({
  properties: {
    showLoading: {
      type: Boolean,
      value: false
    },

    showUserList: {
      type: Boolean,
      value: false
    },

    userList: {
      type: Array,
      value: []
    }
  },

  data: {
    inputValue: '',
  },

  methods: {
    changeInput(event) {
      const inputValue = event.detail.value
      this.setData({
        inputValue
      })
    },

    searchUser(event) {
      let {
        inputValue
      } = this.data

      let {
        showLoading,
        showUserList,
        userList,
      } = this.properties

      if (inputValue === '') {
        this.triggerEvent('updateData', {userList: [], showUserList: false})

        if (userList.length === 0) {
          wx.showToast({
            title: '请输入你要搜索的名称',
            icon: 'none',
          })
        }

        return 
      }

      this.triggerEvent('updateData', {showLoading: true})
      wx.request({
        url: `https://api.github.com/search/users?q=${inputValue}`,
        method: 'get',
        success: (res) => {
          userList = res.data.items.map((item) => {
            return {
              id: item.id,
              name: item.login,
              avatar_url: item.avatar_url,
              url: item.html_url,
            }
          })

          console.log(userList)

          showUserList = true
          this.triggerEvent('updateData', {showUserList, userList})
        },
        fail: () => {

        },
        complete: () => {
          this.triggerEvent('updateData', {showLoading: false})
        }
      })

      this.setData({inputValue: ''})
    }
  }
})